package com.example.team.dao.Imp;

import com.example.team.dao.IJobDao;
import com.example.team.entity.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *作者：代志雄
 * 2023年10月29日23点44分
 */
@Repository
public class IJobDaoImpl implements IJobDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertJob(Job job) {
        return jdbcTemplate.update(
                "insert into job(jobId,JobName,Wage,reward) " +
                        "values(?,?,?,?)"
                ,job.getJobId()
                ,job.getJobName()
                ,job.getWage()
                ,job.getReward()
        );
    }

    @Override
    public int deleteJob(Job job) {
        return jdbcTemplate.update(
                "delete from job " +
                "where JobID=?",job.getJobId());

    }

    @Override
    public int updateJob(Job job) {
        return jdbcTemplate.update("update job set " +
                "JobID=?,JobName=?,Wage=?,reward=?"
                ,job.getJobId()
                ,job.getJobName()
                ,job.getWage()
                ,job.getReward());
    }

    @Override
    public List<Job> getAllJob(Job job) {
        if(job.getJobName()!=null)
            return jdbcTemplate.query("select " +
                "JobID,JobName,Wage,reward " +
                "from job where JobName='"+job.getJobName()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(Job.class));
        else
            return jdbcTemplate.query("select " +
                "JobID,JobName,Wage,reward " +
                "from job",new Object[]{},
                new BeanPropertyRowMapper<>(Job.class));
    }

    @Override
    public Job getJob(Job job) {
        List<Job> result=jdbcTemplate.query("select " +
                                "JobID,JobName,Wage,reward " +
                                "from job where JobName='"+job.getJobName()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Job.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
